package com.gufe.service.admin.impl.mmp;

import com.github.pagehelper.PageHelper;
import com.gufe.entity.Notice;
import com.gufe.mapper.admin.mmp.IndexNoticeMapper;
import com.gufe.result.PageResult;
import com.gufe.service.admin.mmp.IndexNoticeService;
import com.gufe.vo.NoticeVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;


@Service
public class IndexNoticeServiceImpl implements IndexNoticeService {

    @Autowired
    IndexNoticeMapper indexNoticeMapper;

    /*得到分页的通知内容*/
    public PageResult<NoticeVO> getPageNotice(Integer page, Integer pageSize) {

        // 设置分页参数
        PageHelper.startPage(page,pageSize);

        // 执行查询
        List<NoticeVO> noticeVOS = indexNoticeMapper.getNotices();

        // 解析查询结果并封装
        com.github.pagehelper.Page<NoticeVO> p = (com.github.pagehelper.Page<NoticeVO>) noticeVOS;
        return new PageResult<>(p.getTotal(),p.getResult());
    }

    /*添加通知*/
    public void addNotice(Notice notice) {
        notice.setCreatedAt(LocalDateTime.now());
        notice.setUpdatedAt(LocalDateTime.now());
        indexNoticeMapper.insert(notice);
    }

    /*根据id删除通知*/
    public void deleteNoticeById(Integer id) {
        indexNoticeMapper.deleteById(id);
    }

    /*修改通知内容*/
    public void updateNotice(Notice notice) {
        notice.setUpdatedAt(LocalDateTime.now());
        indexNoticeMapper.updateById(notice);
    }

}
